package Stack;

import java.util.Arrays;

public class MyStack {


   public  int[] elem;
   public int UsedSize;

   public static int DEFAULT_CAPACITY=10;//默认容量

   public MyStack() {
      this.elem=new int[DEFAULT_CAPACITY];
   }


   //压栈
   public void push(int date){

      if(isFull()){//判断栈满的情况则扩容
         this.elem= Arrays.copyOf(elem,2*elem.length);
      }
      elem[UsedSize]=date;
      UsedSize++;
   }

   public boolean isFull(){//判断栈满的情况

   return  UsedSize==elem.length;
   }

//出栈并返回
   public int pop(){

      if(isEmpty()){
         throw new isEmptyException("栈为空");
      }

      int date=elem[UsedSize-1];
      UsedSize--;

      return date;
   }

   //获取栈顶元素
   public int peek(){

      if(isEmpty()){
         throw  new isEmptyException("栈为空");
      }

      int date=elem[UsedSize-1];
      return date;
   }
   //栈为空
   public boolean isEmpty(){

      return UsedSize==0;
   }
   //获取栈内有效元素
   public int size(){

      return UsedSize;
   }


   public void display(){

      /*for (int x:elem) {
         System.out.print(x);
      }*/

      int count=-1;
      while (count!=UsedSize-1){
         System.out.print(elem[++count]+" ");
      }
   }


}
